package cn.error1;

public class Demo01 {
    public static void main(String[] args) {
        // test11();
        test12();
    }
    private static String[] names = {"bill","hill","jill"};

    private static void test12() {
        // 模拟数据库中已存在账号
        try {
            checkUsername("bill");
            System.out.println("ok");
        } catch (LoginException e) {
            // throw new RuntimeException(e);
            //处理异常
            e.printStackTrace();
        }
    }

    //判断当前注册账号是否存在
    //因为是编译期异常，又想调用者去处理 所以声明该异常
    public static boolean checkUsername(String uname) throws LoginException {
        for (String name : names) {
            if (name.equals(uname)){
                throw new LoginException("吊毛，"+uname+"你又来注册了");
            }
        }
        return true;
    }

    private static void test11() {
        int[] arr = { 34, 12, 67 };
        int num = 0;
        try {
            num = ArrayTools.getElement(arr, 4);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            // e.printStackTrace();
            // throw new RuntimeException(e);
        }finally {
            System.out.println("finally");
        }
        System.out.println("num=" + num);
        System.out.println("over");
    }
}
